<template>
  <div id="register" :style="background">
    <el-page-header @back="goBack" content="注册管理员">
    </el-page-header>
    <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">

      <el-form-item label="姓名" prop="name">
        <el-input v-model="ruleForm.name"></el-input>
      </el-form-item>

      <el-form-item label="密码" prop="password">
        <el-input type="password" v-model="ruleForm.password" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="confirm">
        <el-input type="password" v-model="ruleForm.confirm" autocomplete="off"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="onSubmit('ruleForm')">提交</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>

export default {
  name: "Register",
  data() {
    let validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'));
      } else {
        if (this.ruleForm.confirm !== '') {
          this.$refs.ruleForm.validateField('checkPass');
        }
        callback();
      }
    };
    let validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'));
      } else if (value !== this.ruleForm.password) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        name: '',
        password: '',
        confirm: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入用户姓名...', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ],
        password: [
          { validator: validatePass, trigger: 'blur' }
        ],
        confirm: [
          { validator: validatePass2, trigger: 'blur' }
        ]
      },
      background: {
        backgroundImage: 'url(' + require('../assets/images/animals/animal5.jpeg') + ')',
        backgroundRepeat: 'no-repeat',
        position: 'fixed',
        backgroundSize: '100% 100%',
        width: '100%',
        height: '100%'
      }
    };
  },
  methods: {
    onSubmit(ruleForm) {
      this.$refs[ruleForm].validate((valid) => {
        if (valid) {
          this.$http.post("/admin/register?name="+this.ruleForm.name+"&password="+this.ruleForm.password).then((response)=>{
            if(response.data.code){
              this.$message({
                message: "恭喜您！" + response.data.extend.message,
                type: 'success'
              });
              this.$router.push('/login');
            }
            else {
              this.$message({
                message: "很抱歉！" + response.data.extend.message,
                type: 'error'
              })
            }
          });
        }
        else {
          this.$message({
            message: "很抱歉！输入数据不合法！",
            type: 'error'
          })
          return false;
        }
      });
    },
    resetForm(ruleForm) {
      this.$refs[ruleForm].resetFields();
    },
    goBack() {
      history.go(-1);
    }
  }
}
</script>

<style>
  .el-form-item label{
    color: white
  }
</style>

<style scoped>
.el-form{
  border: 1px solid #4e4646;
  border-radius: 4px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  width: 400px;
  height: 350px;
  margin: 80px 750px;
}

.el-form-item{
  width: 300px;
  margin-left: 30px;
  margin-top: 30px;
}
</style>